|
1
|
|
|
import { fromJS, List } from 'immutable'; |
|
2
|
|
|
import { generateLastUpdate } from './../../util/lastUpdate'; |
|
3
|
|
|
|
|
4
|
|
|
import { DataSource } from './../../records'; |
|
5
|
|
|
|
|
6
|
|
|
import { getTreePathFromId } from './../../util/getTreePathFromId'; |
|
7
|
|
|
import { moveTreeNode } from './../../util/moveTreeNode'; |
|
8
|
|
|
import { setTreeValue } from './../../util/setTreeValue'; |
|
9
|
|
|
import { treeToFlatList } from './../../util/treeToFlatList'; |
|
10
|
|
|
import { setKeysInData } from './../../util/getData'; |
|
11
|
|
|
import getUpdatedRecord from './../../util/getUpdatedRecord'; |
|
12
|
|
|
|
|
13
|
|
|
export const getUpdatedDataRecord = ( |
|
14
|
|
|
state, stateKey, values = {}, operation = 'setIn' |
|
15
|
|
|
) => { |
|
16
|
|
|
if (operation === 'setIn') { |
|
17
|
|
|
return state[operation]([stateKey], new DataSource(values)); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
else if (operation === 'mergeIn') { |
|
21
|
|
|
return state[operation]([stateKey], state.get(stateKey).merge(values)); |
|
22
|
|
|
} |
|
23
|
|
|
}; |
|
24
|
|
|
|
|
25
|
|
|
export const setData = (state, { |
|
26
|
|
|
currentRecords, data, gridType, stateKey, treeData, total |
|
27
|
|
|
}) => { |
|
28
|
|
|
|
|
29
|
|
|
const keyedData = setKeysInData(data); |
|
30
|
|
|
let keyedCurr; |
|
31
|
|
|
|
|
32
|
|
|
if (currentRecords) { |
|
33
|
|
|
keyedCurr = setKeysInData(currentRecords); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return getUpdatedRecord(state, stateKey, { |
|
37
|
|
View Code Duplication |
|
|
|
|
|
|
|
38
|
|
|
data: keyedData, |
|
39
|
|
|
proxy: keyedData, |
|
40
|
|
|
total: total || keyedData.count(), |
|
41
|
|
|
treeData: fromJS(treeData), |
|
42
|
|
|
gridType: gridType || 'grid', |
|
43
|
|
|
currentRecords: keyedCurr |
|
44
|
|
|
? keyedCurr |
|
45
|
|
|
: keyedData, |
|
46
|
|
|
lastUpdate: generateLastUpdate() |
|
47
|
|
|
}, DataSource); |
|
48
|
|
|
|
|
49
|
|
|
}; |
|
50
|
|
|
|
|
51
|
|
|
export const setPartialTreeData = (state, { |
|
52
|
|
|
data, parentId, showTreeRootNode, stateKey |
|
53
|
|
|
}) => { |
|
54
|
|
|
|
|
55
|
|
|
const tree = state.getIn([stateKey, 'treeData']); |
|
56
|
|
|
const flat = state.getIn([stateKey, 'data']); |
|
57
|
|
|
const pathToNode = [ |
|
58
|
|
|
-1, ...getTreePathFromId(flat, parentId) |
|
59
|
|
|
]; |
|
60
|
|
|
const updatedTree = setTreeValue( |
|
61
|
|
|
tree, pathToNode, { children: data } |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
let updatedFlat = treeToFlatList(updatedTree); |
|
65
|
|
|
|
|
66
|
|
|
if (!showTreeRootNode) { |
|
67
|
|
|
updatedFlat = updatedFlat.shift(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
return getUpdatedRecord(state, stateKey, { |
|
72
|
|
|
|
|
73
|
|
|
data: updatedFlat, |
|
74
|
|
|
currentRecords: updatedFlat, |
|
75
|
|
|
treeData: updatedTree, |
|
76
|
|
|
proxy: updatedFlat, |
|
77
|
|
|
total: updatedFlat.count(), |
|
78
|
|
|
lastUpdate: generateLastUpdate() |
|
79
|
|
|
}, DataSource, 'mergeIn'); |
|
80
|
|
|
|
|
81
|
|
|
}; |
|
82
|
|
|
|
|
83
|
|
|
export const dismissEditor = (state, { stateKey }) => { |
|
84
|
|
|
const previousData = state.getIn([stateKey, 'data']); |
|
85
|
|
|
const previousProxy = state.getIn([stateKey, 'proxy']); |
|
86
|
|
|
let previousTotal = state.getIn([stateKey, 'total']); |
|
87
|
|
|
|
|
88
|
|
|
// upon dismiss, if a new row was in edit |
|
89
|
|
|
// but isn't save, update the total to reflect that |
|
90
|
|
|
if (previousData |
|
91
|
|
|
&& previousProxy |
|
92
|
|
|
&& previousData.size > previousProxy.size) { |
|
93
|
|
|
previousTotal = previousTotal - 1; |
|
94
|
|
|
} |
|
95
|
|
|
const record = state.get(stateKey); |
|
96
|
|
|
|
|
97
|
|
|
if (record) { |
|
98
|
|
|
return getUpdatedRecord(state, stateKey, { |
|
99
|
|
|
data: previousProxy, |
|
100
|
|
|
proxy: previousProxy, |
|
101
|
|
|
currentRecords: previousProxy, |
|
102
|
|
|
isEditing: false, |
|
103
|
|
|
total: previousTotal, |
|
104
|
|
|
lastUpdate: generateLastUpdate() |
|
105
|
|
|
}, DataSource, 'mergeIn'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return state; |
|
109
|
|
|
}; |
|
110
|
|
|
|
|
111
|
|
|
export const removeRow = (state, { stateKey, rowIndex }) => { |
|
112
|
|
|
const existingState = state.get(stateKey); |
|
113
|
|
|
const currentTotal = existingState.get('total'); |
|
114
|
|
|
|
|
115
|
|
|
const remainingRows = state |
|
116
|
|
|
.getIn([stateKey, 'data']) |
|
117
|
|
|
.remove(rowIndex || 0, 1); |
|
118
|
|
|
|
|
119
|
|
|
const updatedTotal = existingState |
|
120
|
|
|
&& currentTotal |
|
121
|
|
|
&& currentTotal > 0 |
|
122
|
|
|
? currentTotal - 1 |
|
123
|
|
|
: 0; |
|
124
|
|
|
|
|
125
|
|
|
<<<<<<< HEAD |
|
126
|
|
|
return getUpdatedDataRecord(state, stateKey, { |
|
127
|
|
|
======= |
|
128
|
|
|
return getUpdatedRecord(state, stateKey, { |
|
129
|
|
|
>>>>>>> cc5039bab2db43dffc3dec6fd8fe7b5bc8c236a4 |
|
130
|
|
|
data: remainingRows, |
|
131
|
|
|
proxy: remainingRows, |
|
132
|
|
|
currentRecords: remainingRows, |
|
133
|
|
|
total: updatedTotal, |
|
134
|
|
|
lastUpdate: generateLastUpdate() |
|
135
|
|
|
<<<<<<< HEAD |
|
136
|
|
|
}, 'mergeIn'); |
|
137
|
|
|
======= |
|
138
|
|
|
}, DataSource, 'mergeIn'); |
|
139
|
|
|
>>>>>>> cc5039bab2db43dffc3dec6fd8fe7b5bc8c236a4 |
|
140
|
|
|
}; |
|
141
|
|
|
|
|
142
|
|
|
export const updateRow = (state, { rowIndex, stateKey, values }) => { |
|
143
|
|
|
|
|
144
|
|
|
const data = state.getIn([stateKey, 'data']); |
|
145
|
|
|
const row = data |
|
146
|
|
|
? data.get(rowIndex) |
|
147
|
|
|
: null; |
|
148
|
|
|
|
|
149
|
|
|
if (!row) { |
|
150
|
|
|
return state; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
const updatedRow = row.merge(values); |
|
154
|
|
|
const updatedData = state.getIn([stateKey, 'data']) |
|
155
|
|
|
.set(rowIndex, updatedRow); |
|
156
|
|
|
|
|
157
|
|
|
<<<<<<< HEAD |
|
158
|
|
|
return getUpdatedDataRecord(state, stateKey, { |
|
159
|
|
|
======= |
|
160
|
|
|
return getUpdatedRecord(state, stateKey, { |
|
161
|
|
|
>>>>>>> cc5039bab2db43dffc3dec6fd8fe7b5bc8c236a4 |
|
162
|
|
|
data: updatedData, |
|
163
|
|
|
proxy: updatedData, |
|
164
|
|
|
currentRecords: updatedData, |
|
165
|
|
|
lastUpdate: generateLastUpdate() |
|
166
|
|
|
<<<<<<< HEAD |
|
167
|
|
|
}, 'mergeIn'); |
|
168
|
|
|
======= |
|
169
|
|
|
}, DataSource, 'mergeIn'); |
|
170
|
|
|
>>>>>>> cc5039bab2db43dffc3dec6fd8fe7b5bc8c236a4 |
|
171
|
|
|
}; |
|
172
|
|
|
|
|
173
|
|
|
export const addNewRow = (state, { rowId, stateKey, rowIndex }) => { |
|
174
|
|
|
const existingState = state.get(stateKey); |
|
175
|
|
|
const isEditing = existingState && existingState.get('isEditing'); |
|
176
|
|
|
let data = existingState && existingState.get('data'); |
|
177
|
|
|
|
|
178
|
|
|
if (existingState && isEditing) { |
|
179
|
|
|
return state; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
let newRow = data |
|
183
|
|
|
&& data.size > 0 |
|
184
|
|
|
&& data.get(0) |
|
185
|
|
|
? data.get(0).reduce((p, i, c) => { return p.set(c, ''); }, fromJS({})) |
|
186
|
|
|
: fromJS({}); |
|
187
|
|
|
|
|
188
|
|
|
newRow = newRow.set('_key', rowId); |
|
189
|
|
|
|
|
190
|
|
|
if (!data) { |
|
191
|
|
|
data = new List(); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
const updatedTotal = existingState |
|
195
|
|
|
&& existingState.get('total') |
|
196
|
|
|
? existingState.get('total') |
|
197
|
|
|
: 0; |
|
198
|
|
|
|
|
199
|
|
|
rowIndex = rowIndex === undefined |
|
200
|
|
|
? 0 |
|
201
|
|
|
: rowIndex; |
|
202
|
|
|
|
|
203
|
|
|
<<<<<<< HEAD |
|
204
|
|
|
return getUpdatedDataRecord(state, stateKey, { |
|
205
|
|
|
======= |
|
206
|
|
|
return getUpdatedRecord(state, stateKey, { |
|
207
|
|
|
>>>>>>> cc5039bab2db43dffc3dec6fd8fe7b5bc8c236a4 |
|
208
|
|
|
data: data.insert(rowIndex, newRow), |
|
209
|
|
|
proxy: data, |
|
210
|
|
View Code Duplication |
isEditing: true, |
|
|
|
|
|
|
211
|
|
|
total: updatedTotal + 1, |
|
212
|
|
|
lastUpdate: generateLastUpdate() |
|
213
|
|
|
<<<<<<< HEAD |
|
214
|
|
|
}, 'mergeIn'); |
|
215
|
|
|
======= |
|
216
|
|
|
}, DataSource, 'mergeIn'); |
|
217
|
|
|
>>>>>>> cc5039bab2db43dffc3dec6fd8fe7b5bc8c236a4 |
|
218
|
|
|
}; |
|
219
|
|
|
|
|
220
|
|
|
export const moveNode = (state, { |
|
221
|
|
|
current, next, showTreeRootNode, stateKey |
|
222
|
|
|
}) => { |
|
223
|
|
|
const nextPath = List(next.path); |
|
224
|
|
|
const tree = state.getIn([stateKey, 'treeData']); |
|
225
|
|
|
const currentPath = List(current.path); |
|
226
|
|
|
|
|
227
|
|
|
const newTreeMove = moveTreeNode( |
|
228
|
|
|
tree, |
|
229
|
|
|
current.index, |
|
230
|
|
|
currentPath, |
|
231
|
|
|
next.index, |
|
232
|
|
|
nextPath |
|
233
|
|
|
); |
|
234
|
|
|
|
|
235
|
|
|
let flatMove = treeToFlatList(newTreeMove); |
|
236
|
|
|
|
|
237
|
|
|
// remove root-node |
|
238
|
|
|
if (!showTreeRootNode) { |
|
239
|
|
|
flatMove = flatMove.shift(); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
<<<<<<< HEAD |
|
243
|
|
|
return getUpdatedDataRecord(state, stateKey, { |
|
244
|
|
|
======= |
|
245
|
|
|
return getUpdatedRecord(state, stateKey, { |
|
246
|
|
|
>>>>>>> cc5039bab2db43dffc3dec6fd8fe7b5bc8c236a4 |
|
247
|
|
|
data: flatMove, |
|
248
|
|
|
currentRecords: flatMove, |
|
249
|
|
|
treeData: newTreeMove, |
|
250
|
|
|
proxy: flatMove, |
|
251
|
|
|
lastUpdate: generateLastUpdate() |
|
252
|
|
|
<<<<<<< HEAD |
|
253
|
|
|
}, 'mergeIn'); |
|
254
|
|
|
======= |
|
255
|
|
|
}, DataSource, 'mergeIn'); |
|
256
|
|
|
>>>>>>> cc5039bab2db43dffc3dec6fd8fe7b5bc8c236a4 |
|
257
|
|
|
}; |
|
258
|
|
|
|
|
259
|
|
|
export const setTreeNodeVisibility = (state, { |
|
260
|
|
|
id, showTreeRootNode, stateKey |
|
261
|
|
|
}) => { |
|
262
|
|
|
|
|
263
|
|
|
const flat = state.getIn([stateKey, 'data']); |
|
264
|
|
|
const tree = state.getIn([stateKey, 'treeData']); |
|
265
|
|
|
|
|
266
|
|
|
const currentVisibility = !!flat |
|
267
|
|
|
.find(node => node.get('_id') === id).get('_hideChildren'); |
|
268
|
|
|
|
|
269
|
|
|
const path = [-1, ...getTreePathFromId(flat, id)]; |
|
270
|
|
|
|
|
271
|
|
|
const updatedTree = setTreeValue( |
|
272
|
|
|
tree, path, { _hideChildren: !currentVisibility } |
|
273
|
|
|
); |
|
274
|
|
|
|
|
275
|
|
|
let updatedList = treeToFlatList(updatedTree); |
|
276
|
|
|
|
|
277
|
|
|
// remove root-node |
|
278
|
|
|
if (!showTreeRootNode) { |
|
279
|
|
|
updatedList = updatedList.shift(); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
<<<<<<< HEAD |
|
283
|
|
|
return getUpdatedDataRecord(state, stateKey, { |
|
284
|
|
|
======= |
|
285
|
|
|
return getUpdatedRecord(state, stateKey, { |
|
286
|
|
|
>>>>>>> cc5039bab2db43dffc3dec6fd8fe7b5bc8c236a4 |
|
287
|
|
|
data: updatedList, |
|
288
|
|
|
currentRecords: updatedList, |
|
289
|
|
|
treeData: updatedTree, |
|
290
|
|
|
proxy: updatedList, |
|
291
|
|
|
lastUpdate: generateLastUpdate() |
|
292
|
|
|
<<<<<<< HEAD |
|
293
|
|
|
}, 'mergeIn'); |
|
294
|
|
|
======= |
|
295
|
|
|
}, DataSource, 'mergeIn'); |
|
296
|
|
|
>>>>>>> cc5039bab2db43dffc3dec6fd8fe7b5bc8c236a4 |
|
297
|
|
|
}; |
|
298
|
|
|
|
|
299
|
|
|
export const saveRow = (state, { rowIndex, stateKey, values }) => { |
|
300
|
|
|
const data = state |
|
301
|
|
|
.getIn([stateKey, 'data']) |
|
302
|
|
|
.set(rowIndex, fromJS(values)); |
|
303
|
|
|
|
|
304
|
|
|
<<<<<<< HEAD |
|
305
|
|
|
return getUpdatedDataRecord(state, stateKey, { |
|
306
|
|
|
======= |
|
307
|
|
|
return getUpdatedRecord(state, stateKey, { |
|
308
|
|
|
>>>>>>> cc5039bab2db43dffc3dec6fd8fe7b5bc8c236a4 |
|
309
|
|
|
data: data, |
|
310
|
|
|
proxy: data, |
|
311
|
|
|
currentRecords: data, |
|
312
|
|
|
lastUpdate: generateLastUpdate() |
|
313
|
|
|
<<<<<<< HEAD |
|
314
|
|
|
}, 'mergeIn'); |
|
315
|
|
|
}; |
|
316
|
|
|
|
|
317
|
|
|
export const sortData = (state, { data, stateKey }) => getUpdatedDataRecord( |
|
318
|
|
|
state, stateKey, { |
|
319
|
|
|
data: data, |
|
320
|
|
|
lastUpdate: generateLastUpdate() |
|
321
|
|
|
}, 'mergeIn'); |
|
322
|
|
|
======= |
|
323
|
|
|
}, DataSource, 'mergeIn'); |
|
324
|
|
|
}; |
|
325
|
|
|
|
|
326
|
|
|
export const sortData = (state, { data, stateKey }) => getUpdatedRecord( |
|
327
|
|
|
state, stateKey, { |
|
328
|
|
|
data: data, |
|
329
|
|
|
lastUpdate: generateLastUpdate() |
|
330
|
|
|
}, DataSource, 'mergeIn'); |
|
331
|
|
|
>>>>>>> cc5039bab2db43dffc3dec6fd8fe7b5bc8c236a4 |
|
332
|
|
|
|
|
333
|
|
|
export const clearFilter = (state, { stateKey }) => { |
|
334
|
|
|
const proxy = state.getIn([stateKey, 'proxy']); |
|
335
|
|
|
const prevData = state.getIn([stateKey, 'data']); |
|
336
|
|
|
const recs = proxy || prevData; |
|
337
|
|
|
|
|
338
|
|
|
<<<<<<< HEAD |
|
339
|
|
|
return getUpdatedDataRecord(state, stateKey, { |
|
340
|
|
|
======= |
|
341
|
|
|
return getUpdatedRecord(state, stateKey, { |
|
342
|
|
|
>>>>>>> cc5039bab2db43dffc3dec6fd8fe7b5bc8c236a4 |
|
343
|
|
|
data: recs, |
|
344
|
|
|
proxy: recs, |
|
345
|
|
|
currentRecords: recs, |
|
346
|
|
|
lastUpdate: generateLastUpdate() |
|
347
|
|
|
<<<<<<< HEAD |
|
348
|
|
|
}, 'mergeIn'); |
|
349
|
|
|
}; |
|
350
|
|
|
|
|
351
|
|
|
export const filterData = (state, { data, stateKey }) => getUpdatedDataRecord( |
|
352
|
|
|
state, stateKey, { |
|
353
|
|
|
data: data, |
|
354
|
|
|
lastUpdate: generateLastUpdate() |
|
355
|
|
|
}, 'mergeIn'); |
|
356
|
|
|
======= |
|
357
|
|
|
}, DataSource, 'mergeIn'); |
|
358
|
|
|
}; |
|
359
|
|
|
|
|
360
|
|
|
export const filterData = (state, { data, stateKey }) => getUpdatedRecord( |
|
361
|
|
|
state, stateKey, { |
|
362
|
|
|
data: data, |
|
363
|
|
|
lastUpdate: generateLastUpdate() |
|
364
|
|
|
}, DataSource, 'mergeIn'); |
|
365
|
|
|
>>>>>>> cc5039bab2db43dffc3dec6fd8fe7b5bc8c236a4 |
|
366
|
|
|
|